package com.tianpengtech.front.web.qiniu;

import com.jfinal.upload.UploadFile;
import com.qiniu.common.QiniuException;
import com.tianpengtech.common.util.JsonUtil;
import com.tianpengtech.common.util.QiNiuUtil;
import com.tianpengtech.front.web.common.controller.BaseController;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2017/5/31 0031.
 */
public class QiNiuController extends BaseController {

    public void index(){
        String token=QiNiuUtil.getToken();
        Map<String,String> tokens=new HashMap<>();
        tokens.put("uptoken",token);
        setAttr("","");
        renderJson(tokens);
    }

    public void upload() throws Exception {
        UploadFile upfile=getFile();
        String category = getPara("category","pc-");
        try {
            String upload = QiNiuUtil.upload(new FileInputStream(upfile.getFile()),category);
            upfile.getFile().delete();
            renderText(upload);
        } catch (Exception e) {
            throw e;
        }

    }
    
    public void deleteQiNiuFile(){
    	String fileKey = getPara("fileKey","");
    	try {
    		//截取字符串 获取key值
    		String f= fileKey.substring(fileKey.lastIndexOf("/")+1);
			QiNiuUtil.deleteQiNiuFile(f);
			renderJson(JsonUtil.printSuccess("删除成功"));
		} catch (QiniuException e) {
			renderJson(JsonUtil.printError("删除失败"));
		}
    }
    
    public static void main(String[] args) {
		String f="http://osyy0ckv9.bkt.clouddn.com/FnmKuDiHmj4U-VvnYAAIKeEL7piy";
		String t =f.substring(f.lastIndexOf("/")+1);
		System.out.println(t);
	}
}
